package com.zhhtech.zhh.ui.ext

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.util.DisplayMetrics
import android.view.Display
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.zhhtech.zhh.ui.AppCore
import com.zhhtech.zhh.ui.utils.Utils
import me.jessyan.autosize.utils.AutoSizeUtils

const val ACTION_USER_LOGIN = "ACTION_USER_LOGIN"
val Int.dp get() = AutoSizeUtils.dp2px(AppCore.app, toFloat())
val Int.sp get() = AutoSizeUtils.sp2px(AppCore.app, toFloat())
val Float.dp get() = (this * AppCore.app.resources.displayMetrics.density + 0.5f).toInt()
val Int.drawable get() = ContextCompat.getDrawable(AppCore.app, this)
val Boolean.toInt get() = if (this) 1 else 0
fun dp2px(dpValue: Int) = (dpValue * AppCore.app.resources.displayMetrics.density + 0.5f).toInt()
fun toastMessageLong(strIdes: Int) = Utils.toastMessageLong(AppCore.app, AppCore.app.getString(strIdes))
fun toastMessageLong(str: String?) = Utils.toastMessageLong(AppCore.app, str)
fun getString(strIdes: Int) = AppCore.app.getString(strIdes)
fun getString(strIdes: Int, vararg formatArgs: Any) = AppCore.app.getString(strIdes, *formatArgs)
fun bindViewClick(listener: View.OnClickListener, vararg views: View?) {
    for (it in views) {
        it?.setOnClickListener(listener)
    }
}

/**
 * 是否是平板
 *
 * @param context 上下文
 * @return 是平板则返回true，反之返回false
 */
fun Context.isPad(): Boolean {
    val wm = getSystemService(Context.WINDOW_SERVICE) as WindowManager
    val display: Display = wm.defaultDisplay
    val dm = DisplayMetrics()
    display.getMetrics(dm)
    val x = Math.pow((dm.widthPixels / dm.xdpi).toDouble(), 2.0)
    val y = Math.pow((dm.heightPixels / dm.ydpi).toDouble(), 2.0)
    val screenInches = Math.sqrt(x + y) // 屏幕尺寸
    return screenInches >= 7.0
}

val Context.isLandscape get(): Boolean = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
fun java.io.Closeable.tryClose() {
    try {
        close()
    } catch (e: Exception) {

    }
}

val EditText.value get() = text.toString()
val TextView.value get() = text.toString()

inline fun <reified T : Activity> Activity.startActivityForResultExt(requestCode: Int, noinline intent: (Intent.() -> Unit)? = null) {
    startActivityForResult(buildIntent<T>(intent), requestCode)
}

inline fun <reified T : Activity> Fragment.startActivityForResultExt(requestCode: Int, noinline intent: (Intent.() -> Unit)? = null) {
    startActivityForResult(requireContext().buildIntent<T>(intent), requestCode)
}

inline fun <reified T : Activity> Activity.startActivityExt(noinline intent: (Intent.() -> Unit)? = null) {
    startActivity(buildIntent<T>(intent))
}

inline fun <reified T : Activity> Fragment.startActivityExt(noinline intent: (Intent.() -> Unit)? = null) {
    startActivity(requireContext().buildIntent<T>(intent))
}

inline fun <reified T : Activity> Context.startActivityExt(noinline intent: (Intent.() -> Unit)? = null) {
    startActivity(buildIntent<T>(intent))
}

inline fun <reified T : Context> Context.buildIntent(noinline initIntent: (Intent.() -> Unit)? = null): Intent {
    val intent = Intent(this, T::class.java)
    if (initIntent != null) {
        intent.apply(initIntent)
    }
    return intent
}

interface DefaultLifecycleObserver : LifecycleEventObserver {

    override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
        when (event) {
            Lifecycle.Event.ON_CREATE -> onCreate(source)
            Lifecycle.Event.ON_START -> onStart(source)
            Lifecycle.Event.ON_RESUME -> onResume(source)
            Lifecycle.Event.ON_PAUSE -> onPause(source)
            Lifecycle.Event.ON_STOP -> onStop(source)
            Lifecycle.Event.ON_DESTROY -> onDestroy(source)
            Lifecycle.Event.ON_ANY -> throw IllegalArgumentException("ON_ANY must not been send by anybody")
        }
    }

    fun onCreate(owner: LifecycleOwner) {}

    fun onStart(owner: LifecycleOwner) {}

    fun onResume(owner: LifecycleOwner) {}

    fun onPause(owner: LifecycleOwner) {}

    fun onStop(owner: LifecycleOwner) {}

    fun onDestroy(owner: LifecycleOwner) {}
}